<!--
 * @description: 密码模式
 * @Author: Islands
 * @Date: 2024-06-15 03:18:01
 * @LastEditTime: 2024-06-17 21:32:54
-->
<script lang="ts" setup>
import { ref, reactive, toRefs, onMounted } from "vue";
import { $t } from "@/plugins/i18n/index";
import { User, Lock } from "@element-plus/icons-vue";
import type { FormInstance, FormRules } from "element-plus";
import { ILoginData } from "@/api/types/login.d";
import { AES_Encrypt, AES_Decrypt } from "@/utils/aes-crypto/index";
import { pwdLogin } from "../curd";
const rememberMe = ref(false);
const ruleFormRef = ref<FormInstance>();

const { loginForm, rulesForm } = toRefs(
  reactive({
    loginForm: {} as ILoginData,
    rulesForm: {
      username: [
        {
          required: true,
          message: $t(`placeholder.login.username`, {
            prefix: $t(`placeholder.enter`),
          }),
          trigger: "click",
        },
      ],
      password: [
        {
          required: true,
          message: $t(`placeholder.login.password`, {
            prefix: $t(`placeholder.enter`),
          }),
          trigger: "click",
        },
      ],
    } as FormRules,
  })
);

onMounted(() => {
  let item = localStorage.getItem("REMEMBER");
  if (item) {
    let decrypt = AES_Decrypt(item);
    if (decrypt) {
      loginForm.value.username = decrypt.split(":")[0];
      loginForm.value.password = decrypt.split(":")[1];
      rememberMe.value = true;
    }
  }
});

const loginSubmit = async (
  formEl: FormInstance | undefined,
  loginForm: ILoginData,
  rememberMe: boolean
) => {
  if (!formEl) return;
  await formEl.validate((valid) => {
    if (valid) {
      if (rememberMe) {
        let str = loginForm.username + ":" + loginForm.password;
        localStorage.setItem("REMEMBER", AES_Encrypt(str));
      } else {
        localStorage.remove("REMEMBER");
      }
      pwdLogin(loginForm);
    }
  });
};
</script>
<template>
  <el-form ref="ruleFormRef" :model="loginForm" :rules="rulesForm" size="large">
    <el-form-item prop="username">
      <el-input
        :prefix-icon="User"
        v-model="loginForm.username"
        :placeholder="$t(`placeholder.login.username`)"
      />
    </el-form-item>
    <el-form-item prop="password">
      <el-input
        :prefix-icon="Lock"
        show-password
        type="password"
        v-model="loginForm.password"
        :placeholder="$t(`placeholder.login.password`)"
      />
    </el-form-item>
    <el-form-item>
      <div class="between-space-justify" style="width: 100%">
        <el-checkbox v-model="rememberMe">{{
          $t(`button.rememberMe`)
        }}</el-checkbox>
        <el-button disabled text>{{ $t(`button.forgetPassword`) }} </el-button>
      </div>
    </el-form-item>
    <el-form-item>
      <el-button
        style="width: 100%"
        type="success"
        auto-insert-space
        dark
        round
        @click="loginSubmit(ruleFormRef, loginForm, rememberMe)"
        >{{ $t(`button.login`) }}</el-button
      >
    </el-form-item>
  </el-form>
</template>
